òɾۿûѧϰʹá
ԭַhttps://www.joinquant.com/post/14460

ԭһ˵ʽ鵽ԭĺ߽ۡ


ԭĲԴ£

# 뺯
import jqdata

# ʼ趨ҪĹƱ׼ȵ
def initialize(context):
    # һȫֱ, ҪĹƱ
    # 000001(Ʊ:ƽ)
    g.security = '000333.XSHE'
    # 趨300Ϊ׼
    set_benchmark('000300.XSHG')
    # ̬Ȩģʽ(ʵ۸)
    set_option('use_real_price', True)

# ÿλʱ(ز,ÿһ,,ÿӵһ)һ
def handle_data(context, data):
    security = g.security
    # ȡƱ̼
    close_data1 = attribute_history(security, 20, '1d', ['close'])
    close_data2 = attribute_history(security, 10, '1d', ['close'])
    # ȡùȥƽ۸
    MA10 = close_data2['close'].mean()
    MA5 = close_data1['close'].mean()
    # ȡһʱ۸
    current_price = close_data1['close'][-1]
    # ȡõǰֽ
    cash = context.portfolio.cash

    # һʱ۸߳ƽ1%, ȫ
    if current_price > 1.01*MA10:
        #  cash Ʊ
        order_value(security, cash)
        # ¼
        log.info("Buying %s" % (security))
    # һʱ۸ƽ, ղ
    elif current_price < MA5 and context.portfolio.positions[security].closeable_amount > 0:
        # йƱ,ʹֻƱճΪ0
        order_target(security, 0)
        # ¼
        log.info("Selling %s" % (security))
    # һʱ۸
    record(stock_price=current_price)
